using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace SpoonCore.Message.Info.Core
{
	class CoreInfoHandler
	{
		public static ConfirmationInfo Confirmation { get { return new ConfirmationInfo(); } }

		private ConfirmationManager _confirmationManager;
		private SpoonClient _client;

		public CoreInfoHandler(InfoMessageHandler handler, SpoonClient client)
		{
			_client = client;

			// Confirmation
			_confirmationManager = new ConfirmationManager(_client);
			_confirmationManager.RegisterFaildHandler<JoinMessage>(JoinMessageConfirmationFaild);
			handler.RegisterHandler(new InfoMessageHandler.OnInfoReceiveHandler<ConfirmationInfo>(ValidationReceive), new InfoMessageHandler.OnInfoSendHandler<ConfirmationInfo>(ValidationSend));
		}

		private void JoinMessageConfirmationFaild(JoinMessage message)
		{
			Console.ForegroundColor = ConsoleColor.Cyan;
			Console.WriteLine("Resend joinmessage");
			Console.ForegroundColor = ConsoleColor.Gray;
			_client.Send(message.JoinInfo.Id, message);
		}

		private void ValidationSend(IMessage msg, IPEndPoint dest, ConfirmationInfo info)
		{
			_confirmationManager.Register(msg, dest, info);
		}

		private void ValidationReceive(ConfirmationInfo info, IPEndPoint source)
		{
			_confirmationManager.Receive(info, source);
		}
	}
}
